// **********************************************
// * APM_Init.sqf [1.0]                         *
// * Init module of AP Mines                    *
// * by Prodavec                                *
// **********************************************

// DEFINITIONS
// ----------------------------------------------

// PRE-INIT
// ----------------------------------------------

// VARIABLES
// ----------------------------------------------
APM_ExplosionTriggerArray = [];

// FUNCTIONS
// ----------------------------------------------
// Has unit grenade or not
fnc_APM_hasMine =
{
	private "_magazines";
	
	_magazines = magazines _this;
	if (('HandGrenade_East' in _magazines) || ('HandGrenade_West' in _magazines)) then
	{
		true
	}
	else
	{
		false
	};
};

// Functions that checks explosion trigger conditions
fnc_APM_CheckTriggerConditions =
{
	private
	[
		"_explosionTriggerIndex",
		"_explosionTrigger",
		"_unitList",
		"_explosiveDevices"
	];
	_explosionTriggerIndex = _this select 0;
	_unitList = _this select 1;

	_explosionTrigger = APM_ExplosionTriggerArray select _explosionTriggerIndex;

	_return = false;

	// if unit moves over speed - return TRUE
	{
		if (((abs (speed _x)) >=5)) then
		{
			_return = true;
		};
	} forEach _unitList;
	
	// If there's no more explosive device - return TRUE
	_explosiveDevices = (nearestObjects [getPosATL _explosionTrigger, ["Explosive"], 1]);
	if (count _explosiveDevices < 1) then
	{
		_return = true;
	};

	_return
};

// Function is executed from the explosion trigger
fnc_APM_Explode =
{
	private
	[
		"_explosionTriggerIndex",
		"_explosionTrigger",
		"_explosiveDevices",
		"_position",
		"_grenade"
	];
	_explosionTriggerIndex = _this select 0;

	//player groupChat "fnc_APM_Explode: executed";

	// Getting trigger from trigger array by num
	_explosionTrigger = APM_ExplosionTriggerArray select _explosionTriggerIndex;
	_position = getPosATL _explosionTrigger;

	// Looking for explosive devices around of trigger
	_explosiveDevices = (nearestObjects [_position, ["Explosive"], 1]);
	
	// If we have explosives - delete it and spawn FX, if we have no explosives - just delete trigger
	if (count _explosiveDevices > 0) then
	{
		deleteVehicle (_explosiveDevices select 0); // Delete explosive device
		//_grenade = createVehicle ["Bo_GBU12_LGB", _position, [], 0, "NONE"];
		_grenade = createVehicle ["grenade", _position, [], 0, "NONE"];
		//_grenade = createVehicle ["SmokeShell", _position, [], 0, "NONE"];
	};
	deleteVehicle _explosionTrigger;
};

// Function deletes <NULL-OBJ> entries inside of APM_ExplosionTriggerArray and
// assigns new element numbers to trigger's Statement field.
fnc_APM_ArrangeTriggerArray =
{
	private
	[
		"_newTriggerArray",
		"_i"
	];

	_newTriggerArray = [];
	_i = 0;
	{
		if (!isNull _x) then
		{
			_newTriggerArray set [_i, _x];
			call compile format ["_x setTriggerStatements ['[%1, thislist] call fnc_APM_CheckTriggerConditions', '[%1] spawn fnc_APM_Explode', ''];", _i];
			_i = _i + 1;
		};
	} forEach APM_ExplosionTriggerArray;
	APM_ExplosionTriggerArray = _newTriggerArray;
};

// MAIN
// ----------------------------------------------

// Add Actions for player only
if (isMultiplayer && isServer && isDedicated) exitWith {};
waitUntil {sleep 0.5; player == player};

_canPlant = player getVariable "APM_ICanPlant"; if (isNil "_canPlant") then {_canPlant = false;};
_canDisarm = player getVariable "APM_ICanDisarm"; if (isNil "_canDisarm") then {_canDisarm = false;};

if (_canPlant) then
{
	player addAction ["Plant AP mine", "ap_mines\APM_CreateMine.sqf", [], 0, false, true, "", "player call fnc_APM_hasMine"];
};

if (_canDisarm) then
{
	player addAction ["Disarm AP mine", "ap_mines\APM_DisarmMine.sqf", [], 0, false, true, "", ""];
};